package controllers;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import models.Attachment;
import models.Picture;

import org.apache.commons.io.FileUtils;
import play.Play;
import play.mvc.Controller;
import play.mvc.With;
import service.AttachmentService;
import util.ResourceOperate;

@With(Secure.class)
public class AttachmentAction extends Controller {

	public static String[] types = { "doc", "docx", "xls", "xlsx", "ppt",
			"pptx", "pdf", "swf", "rar", "zip", "txt", "xml", "html", "htm",
			"css", "js", "db", "dat" };

	/**
	 * 附件全查
	 */

	@Check("ResourceManage")
	public static void list() {
		List<Attachment> attList = AttachmentService.findAll();
		render(attList);
	}

	@Check("ResourceManage")
	public static void blank() {
		render();
	}
	
	/**
	 * 添加
	 * 
	 * @param name
	 * @param file
	 * @param dis
	 */
	@Check("ResourceManage")
	public static void createAtt(File file, String attDis) {
		try{
			boolean b = (Boolean) ResourceOperate.upload(file,types).get(0);
			if(b){
				String uploadPath = ResourceOperate.upload(file, types).get(1).toString();
				Attachment att = new Attachment();
				att.attName = ResourceOperate.splitStr(file.getName()).get(0).toString(); // 拿到文件名
				att.attPath = uploadPath;
				att.attDis = attDis;
				AttachmentService.create(att);
				flash.success("上传成功 ！");
				list();
			}else{
				flash.success("上传失败 ！");
				list();
			}
		} catch (Exception e) {
			blank();
			flash.success("上传失败 ！");
			e.printStackTrace();
			list();
		}
	}

	/**
	 * 修改信息
	 * 
	 * @param id
	 */
	@Check("ResourceManage")
	public static void show(long id) {
		Attachment att = AttachmentService.findById(id);
		render(att);
	}

	/**
	 * 修改
	 * 
	 * @param name
	 * @param path
	 * @param dis
	 * @param file
	 * @param id
	 */
	@Check("ResourceManage")
	public static void saveAtt(String attName, String attPath, String attDis,
			File file, long id) {
		try {

			Attachment att = AttachmentService.findById(id);
			String oldName = att.attName;
			att.attName = attName;
			att.attDis = attDis;
			if (file != null) {
				boolean bb = (Boolean) ResourceOperate.upload(file,types).get(0);
				if(bb){
					boolean b = ResourceOperate.deleteFile(Play.applicationPath + attPath);
					if (b) {
						String uploadPath = ResourceOperate.upload(file, types).get(1).toString();
						String ends =  ResourceOperate.splitStr(uploadPath).get(1).toString();
						att.attPath = uploadPath.replace(file.getName().toString(), attName+"."+ends); // 原名
						ResourceOperate.reName(uploadPath, attName); 		// 修改文件夹中的文件名称
					}
					AttachmentService.save(att); 	// 将修改后的数据保存到数据库中
					flash.success("修改成功 ！");
					list();
				}else{
					flash.success("修改失败 ！");
					list();
				}	
			} else {
				String path = attPath.replace(oldName, attName); // 将目录下的文件名称修改
				att.attPath = path;
				ResourceOperate.reName(attPath, attName);
				AttachmentService.save(att); 	// 将修改后的数据保存到数据库中
				flash.success("修改成功 ！");
				list();
			}
				
		} catch (Exception e) {
			flash.success("修改失败 ！");
			list();
			e.printStackTrace();
		}
	}

	/**
	 * 删除
	 * 
	 * @param id
	 */
	@Check("ResourceManage")
	public static void deleteAtt(Long id) {
		String attPath = Play.applicationPath
				+ AttachmentService.findById(id).attPath;
		boolean b = ResourceOperate.deleteFile(attPath);
		if (b) {
			AttachmentService.delete(id);
			flash.success("删除成功 ！");
		} else {
			flash.success("删除失败 ！");
		}

		list();
	}

}
